<?php

namespace Admin\Controller;

use Think\Controller;

class FriendController extends AdminController
{

    public function index()
    {
        $title = I('title');

        if (is_numeric($title)) {
            $map['id|title'] = array(intval($title), array('like', '%' . $title . '%'), '_multi' => true);
        } else {
            $map['title'] = array('like', '%' . (string) $title . '%');
        }

        $list = $this->lists('Friend', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '友链信息';
        $this->display();
    }
    public function add()
    {
        if (IS_POST) {
            $_POST['pic'] = $this->Upload('pic');
            $friend       = D('Friend');
            $data         = $friend->create();
            if ($data) {
                //var_dump($data);die;
                $friend->add($data);
                $this->success('添加成功');
            } else {
                $this->error($friend->getError());
            }

        } else {
            $this->display();
        }
    }
    public function update()
    {
        $friend = D('Friend');
        
        if (IS_GET) {
            $res = $friend->find(I('get.id'));
            $this->assign('res', $res);
            $this->display();
        } else {
            if ($_FILES['pic']['error'] != 4) {
                $_POST['pic'] = $this->upload('pic');
            }

            $data = $friend->create();
            if ($data) {
                $friend->where(['id' => I('post.id')])->save($data);
                $this->success('修改成功', U('index'));

            } else {
                $this->error($friend->getError());
            }

        }

    }
    public function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];
        }
    }
    public function changeStatus($method = null)
    {
        $id = array_unique((array) I('id', 0));
        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'hide':
                $this->forbid('Friend', $map);
                break;
            case 'show':
                $this->resume('Friend', $map);
                break;
            case 'del':
                D('Friend')->where($map)->delete();
                $this->success('删除成功');
                break;
            default:
                $this->error('参数非法');
        }
    }

}
